
Namespace CommonDialog

    Public Class SaveFileDialog

#Region " Declarations "

        Private _bolAddExtension As Boolean = True
        Private _bolCheckFileExists As Boolean = True
        Private _bolCheckPathExists As Boolean = True
        Private _bolCreatePrompt As Boolean = False
        Private _bolDefaultExtension As String = String.Empty
        Private _bolOverWritePrompt As Boolean = True
        Private _bolSupportMultiDottedExtensions As Boolean = True
        Private _bolValidateNames As Boolean = True
        Private _strFileName As String = String.Empty
        Private _strFilter As String = String.Empty
        Private _strInitialDirectory As String = String.Empty
        Private _strTitle As String = "Select File To Save"

#End Region

#Region " Properties "

        ''' <summary>
        ''' Gets or sets a value indicating whether the dialog box automatically adds an extension to a file name if the user omits the extension. The default is true.
        ''' </summary>
        Public Property AddExtension() As Boolean
            Get
                Return _bolAddExtension
            End Get
            Set(ByVal Value As Boolean)
                _bolAddExtension = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether a file dialog displays a warning if the user specifies a filename that does not exist. The default is true.
        ''' </summary>
        Public Property CheckFileExists() As Boolean
            Get
                Return _bolCheckFileExists
            End Get
            Set(ByVal Value As Boolean)
                _bolCheckFileExists = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value that specifies whether warnings are displayed if the user types invalid paths and filenames. The default is true.
        ''' </summary>
        Public Property CheckPathExists() As Boolean
            Get
                Return _bolCheckPathExists
            End Get
            Set(ByVal Value As Boolean)
                _bolCheckPathExists = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether the dialog box prompts the user for permission to create a file if the user specifies a file that does not exist. The default is false.
        ''' </summary>
        Public Property CreatePrompt() As Boolean
            Get
                Return _bolCreatePrompt
            End Get
            Set(ByVal Value As Boolean)
                _bolCreatePrompt = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the default file name extension. 
        ''' </summary>
        Public Property DefaultExtension() As String
            Get
                Return _bolDefaultExtension
            End Get
            Set(ByVal Value As String)
                _bolDefaultExtension = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets a string that is the full path of the file selected in the file dialog. The default is Empty. 
        ''' </summary>
        Public ReadOnly Property FileName() As String
            Get
                Return _strFileName
            End Get
        End Property

        ''' <summary>
        ''' Gets or sets the current filename filter string, which determines the choices that appear in the "Save as file type" box at the bottom of a file dialog.  Filter string consists of one or more pairs of string values, all separated by the pipe symbol ( | ).  Example : "Saved Report PDF Files(*.pdf)|*.pdf|All Files (*.*)|*.*"
        ''' </summary>
        Public Property Filter() As String
            Get
                Return _strFilter
            End Get
            Set(ByVal Value As String)
                _strFilter = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the initial directory displayed by the file dialog box. 
        ''' </summary>
        Public Property InitialDirectory() As String
            Get
                Return _strInitialDirectory
            End Get
            Set(ByVal Value As String)
                _strInitialDirectory = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether the Save As dialog box displays a warning if the user specifies a file name that already exists. The default is true.
        ''' </summary>
        Public Property OverWritePrompt() As Boolean
            Get
                Return _bolOverWritePrompt
            End Get
            Set(ByVal Value As Boolean)
                _bolOverWritePrompt = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets whether the dialog box supports displaying and saving files that have multiple file name extensions.  The default is true.
        ''' </summary>
        Public Property SupportMultiDottedExtensions() As Boolean
            Get
                Return _bolSupportMultiDottedExtensions
            End Get
            Set(ByVal Value As Boolean)
                _bolSupportMultiDottedExtensions = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets the text that appears in the title bar of a file dialog. The default value is "Select File To Save"
        ''' </summary>
        Public Property Title() As String
            Get
                Return _strTitle
            End Get
            Set(ByVal Value As String)
                _strTitle = Value
            End Set
        End Property

        ''' <summary>
        ''' Gets or sets a value indicating whether the dialog accepts only valid Win32 filenames. The default values is true.
        ''' </summary>
        Public Property ValidateNames() As Boolean
            Get
                Return _bolValidateNames
            End Get
            Set(ByVal Value As Boolean)
                _bolValidateNames = Value
            End Set
        End Property

#End Region

#Region " Constructors "

        Public Sub New()
        End Sub

#End Region

#Region " Methods "

        ''' <summary>
        ''' Displays the save file dialog for the current operating system.  Returns true if the user selected or entered a file name.  Read the FileName property for the selected file.
        ''' </summary>
        Public Function ShowDialog() As Boolean

            Dim objDialogSaveFile As New VistaDialogs.VistaSaveFileDialog
            Using objDialogSaveFile
                objDialogSaveFile.AddExtension = Me.AddExtension
                objDialogSaveFile.CheckFileExists = Me.CheckFileExists
                objDialogSaveFile.CheckPathExists = Me.CheckPathExists
                objDialogSaveFile.CreatePrompt = Me.CreatePrompt
                objDialogSaveFile.DefaultExt = Me.DefaultExtension
                objDialogSaveFile.Filter = Me.Filter
                objDialogSaveFile.InitialDirectory = Me.InitialDirectory
                objDialogSaveFile.OverwritePrompt = Me.OverWritePrompt
                objDialogSaveFile.ShowHelp = False
                objDialogSaveFile.SupportMultiDottedExtensions = Me.SupportMultiDottedExtensions
                objDialogSaveFile.Title = Me.Title
                objDialogSaveFile.ValidateNames = Me.ValidateNames

                Dim objResult As System.Windows.Forms.DialogResult = objDialogSaveFile.ShowDialog

                If objResult = Forms.DialogResult.OK Then
                    _strFileName = objDialogSaveFile.FileName
                    objDialogSaveFile.Dispose()
                    Return True

                Else
                    'user cancelled
                    objDialogSaveFile.Dispose()
                    Return False
                End If

            End Using
        End Function

#End Region

    End Class

End Namespace
